package com.abc.robot01.activity

import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.lifecycle.lifecycleScope
import com.abc.robot01.R
import com.abc.robot01.retrofit.RetrofitService
import com.abc.robot01.vivemodel.Robot
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlin.coroutines.cancellation.CancellationException


/**
 * 户型调整
 */
class HuXingTiaoZheng2 : BaseVoiceActivity() {
    private var job: Job? = null;
    val show = mutableStateOf(false);

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            val show by remember { show }
            MaterialTheme {
                Box {
                    Image(painter = painterResource(id = R.drawable.z1), contentDescription = "")
//                    Row(modifier = Modifier.padding(30.px)) {
//                        BackButton {
//                        }
//                    }
                    Box(
                        modifier = Modifier.fillMaxSize(),
                        contentAlignment = Alignment.Center // 将内容居中
                    ) {
                        Content()
                    }
                    Box(
                        modifier = Modifier
                            .padding(30.px)
                            .align(Alignment.BottomStart)
                    ) {
                        BackButton()
                    }
                    if (show) {
                        MinimalDialog2 {
                            job?.cancel()
                            startActivity<BaseActivity>() {
                                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
                            }
                            finish()
                        }
                    }
                }
            }
        }
    }

    @Composable
    private fun Content() {
        Row {
            Frame109()
            Spacer(modifier = Modifier.width(200.px))
            Frame110()
        }
    }

    @Composable
    fun Frame109() {
        Column(
            horizontalAlignment = Alignment.CenterHorizontally,// 水平居中
            modifier = Modifier
                .clickable {
                    job = lifecycleScope.launch {
                        try {
                            val res = RetrofitService.instance.addConfigToHouse(
                                accessToken = Robot.accessToken,
                                designPackageId = intent.getStringExtra("designPackageId")!!,
                                companyId = Robot.companyId,
                                userId = Robot.userId,
                                type = 13,
                                housePlanDataId = intent.getStringExtra("housePlansDataId")!!
                            )
                            if (res.code == 200) {
                                show.value = true
                                delay(5000)
                                startActivity<BaseActivity>() {
                                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
                                }
                                finish()
                            } else {
                                Toast
                                    .makeText(
                                        this@HuXingTiaoZheng2,
                                        "请求出错,请检查网络",
                                        Toast.LENGTH_LONG
                                    )
                                    .show()
                            }
                        } catch (e: Exception) {
                            e.printStackTrace()
                            if(e is CancellationException){

                            } else
                            Toast
                                .makeText(
                                    this@HuXingTiaoZheng2,
                                    "请求出错,请检查网络:" + e.message!!,
                                    Toast.LENGTH_LONG
                                )
                                .show()
                        }
                    }
                }
                .size(500.px) // 宽度和高度
                .shadow(
                    elevation = 8.px, // 阴影大小
                    shape = RectangleShape,
                    clip = false
                )
                .background(Color.White)
                .clip(RoundedCornerShape(20.px)) // 圆角处理
        ) {
            Spacer(modifier = Modifier.height(100.px))
            Image(painter = painterResource(id = R.drawable.abc), contentDescription = "")
            Spacer(modifier = Modifier.height(70.px))
            Text(
                text = "进入3D场景替换",
                style = TextStyle(
                    fontFamily = FontFamily.Serif, // 设置字体（如果你有其他的字体文件，加载它）
                    fontWeight = FontWeight.Bold, // 设置字体粗细
                    fontSize = 48.mysp, // 设置字体大小
                    lineHeight = 70.mysp, // 设置行高，等于文本框的高度
                    color = Color(0xFF2C72E5), // 设置文本颜色
                    textAlign = TextAlign.Center
                ),
            )
            Spacer(modifier = Modifier.height(21.px))
            Text(
                text = "至大屏端选择任务进行",
                style = TextStyle(
                    fontFamily = androidx.compose.ui.text.font.FontFamily.Serif, // 设置字体（如果你有其他的字体文件，加载它）
                    fontWeight = FontWeight.Bold, // 设置字体粗细
                    fontSize = 20.mysp, // 设置字体大小
                    lineHeight = 29.mysp, // 设置行高，等于文本框的高度
                    color = MYBLACK.copy(alpha = 0.6f), // 设置颜色，rgba(51, 51, 51, 0.6)
                    textAlign = TextAlign.Center, // 文本居中对齐
                )
            )

        }
    }

    @Composable
    fun Frame110() {
        Column(
            horizontalAlignment = Alignment.CenterHorizontally,// 水平居中
            modifier = Modifier
                .clickable {
                    this@HuXingTiaoZheng2.startActivity<RenderActivity> {
                        putExtra("name", "name is null")
                        putExtra("taskId", "")
                        putExtra("designPackageId", intent.getStringExtra("designPackageId"))
                        putExtra("housePlansDataId", intent.getStringExtra("housePlansDataId"))
                        putExtra("housePlansId", intent.getStringExtra("housePlansId"))
                    }
                }
                .size(500.px) // 宽度和高度
                .shadow(
                    elevation = 8.px, // 阴影大小
                    shape = RectangleShape,
                    clip = false
                )
                .background(Color.White)
                .clip(RoundedCornerShape(20.px)) // 圆角处理
        ) {
            Spacer(modifier = Modifier.height(100.px))
            Image(painter = painterResource(id = R.drawable.qaw), contentDescription = "")
            Spacer(modifier = Modifier.height(70.px))
            Text(
                text = "进行渲染支付",
                style = TextStyle(
                    fontFamily = FontFamily.Serif, // 设置字体（如果你有其他的字体文件，加载它）
                    fontWeight = FontWeight.Bold, // 设置字体粗细
                    fontSize = 48.mysp, // 设置字体大小
                    lineHeight = 70.mysp, // 设置行高，等于文本框的高度
                    color = Color(0xFF2C72E5), // 设置文本颜色
                    textAlign = TextAlign.Center
                ),
            )
        }
    }

    override fun onPause() {
        super.onPause()
        job?.cancel()
    }

    companion object {
        val TAG = "过渡页面"
    }
}
